import type {Executor} from '../';
import type {Document, EmbDTO} from '../model/static/';

export class DocumentController {

    constructor(private executor: Executor) {
    }

    /**
     * 嵌入文件
     *
     * @parameter {DocumentControllerOptions['embedding']} options
     * - file 待嵌入的文件
     * @return 是否成功
     */
    embedding: (options: DocumentControllerOptions['embedding']) => Promise<
        boolean
    > = async (options) => {
        let _uri = '/document/embedding';
        const _formData = new FormData();
        const _body = options.body;
        _formData.append("file", _body.file);
        return (await this.executor({uri: _uri, method: 'POST', body: _formData})) as Promise<boolean>;
    }

    /**
     * 嵌入文本
     *
     * @return 是否成功
     */
    embeddingText: (options: DocumentControllerOptions['embeddingText']) => Promise<
        boolean
    > = async (options) => {
        let _uri = '/document/embeddingText';
        return (await this.executor({uri: _uri, method: 'POST', body: options.body})) as Promise<boolean>;
    }

    /**
     * 查询向量数据库
     *
     * @parameter {DocumentControllerOptions['query']} options
     * - query 用户的提问
     * @return 匹配到的文档
     */
    query: (options: DocumentControllerOptions['query']) => Promise<
        Array<Document>
    > = async (options) => {
        let _uri = '/document/query';
        let _separator = _uri.indexOf('?') === -1 ? '?' : '&';
        let _value: any = undefined;
        _value = options.query;
        _uri += _separator
        _uri += 'query='
        _uri += encodeURIComponent(_value);
        _separator = '&';
        return (await this.executor({uri: _uri, method: 'GET'})) as Promise<Array<Document>>;
    }
}

export type DocumentControllerOptions = {
    'embedding': {
        body: {
            file: File
        }
    },
    'embeddingText': {
        body: EmbDTO
    },
    'query': {
        /**
         * 用户的提问
         */
        query: string
    }
}